package com.xherorlclass;


import com.xherorlclass.discovery.Registry;
import com.xherorlclass.discovery.RegistryConfig;
import com.xherorlclass.exceptions.NetworkException;
import com.xherorlclass.proxy.handler.RpcConsumerInvocationHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author ：Xherorl
 * @date ：Created in 2023/8/24 10:51
 * @description：
 */
@Slf4j
public class ReferenceConfig<T> {

    private Class<T> interfaceRef;
    private Registry registry;
    private String group;


    /*
    * 代理设计模式，生成一个api接口的代理对象
    * */
    public T get(){
        // 此处一定是使用代理对象完成了一些工作
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class[] classes = {interfaceRef};

        RpcConsumerInvocationHandler handler = new RpcConsumerInvocationHandler(registry, interfaceRef, group);

        // 使用动态代理生成代理对象
        Object helloProxy = Proxy.newProxyInstance(classLoader, classes, handler);

        return (T)helloProxy;

    }


    public void setInterface(Class<T> interfaceRef) {
        this.interfaceRef = interfaceRef;
    }

    public Class<T> getInterface() {
        return interfaceRef;
    }

    public void setInterfaceRef(Class<T> interfaceRef) {
        this.interfaceRef = interfaceRef;
    }

    public Registry getRegistry() {
        return registry;
    }

    public void setRegistry(Registry registry) {
        this.registry = registry;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getGroup() {
        return group;
    }
}
